﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using System.Collections.Generic;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Product.Atl {
    public partial class EditAtl : PageSupport {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        String AtlKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            AtlKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                this.hid_CategoryKey.Value = Request.QueryString["cid"];
                InitForm();
                if (!String.IsNullOrEmpty(AtlKey)) {
                    FillForm(AtlKey);
                }
                String path = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, CodeTypeDef.Category.Product, this.hid_CategoryKey.Value, " / ");
                this.txt_CategoryName.Text = (String.IsNullOrEmpty(path)) ? "所有分类" : path;
            }
        }

        private void InitForm() {
            ClientBiz.BindCodeList(ref this.ddl_FieldType, CodeTypeDef.FormField.Key, true, null);

        }

        /// <summary>
        /// 表单初始化数据
        /// </summary>
        /// <param name="typeKey"></param>
        private void FillForm(String typeKey) {
            VProductAtlEO eo = biz.Load<VProductAtlEO>(typeKey);
            this.ddl_FieldType.SelectedValue = eo.FieldType;
            this.txt_AtlName.Text = eo.AtlName;
            this.txt_OrderNumber.Text = eo.OrderNumber.ToString();
            this.txt_DefaultVaue.Text = eo.DefaultValue;
            this.hid_CategoryKey.Value = eo.CategoryKey;
            this.ral_Queriable.SelectedValue = eo.IsQueriable + "";
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e) {
            //---- 增加新项 ---------------------------------
            if (String.IsNullOrEmpty(AtlKey)) {
                TProductAtlEO eo = new TProductAtlEO();
                eo.AtlDesc = this.txt_AltDesc.Text;
                eo.AtlName = this.txt_AtlName.Text;
                eo.CategoryKey = this.hid_CategoryKey.Value;
                eo.DefaultValue = this.txt_DefaultVaue.Text;
                eo.FieldType = this.ddl_FieldType.SelectedValue;
                eo.IsQueriable = Convert.ToInt32(this.ral_Queriable.SelectedValue);
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                biz.Save<TProductAtlEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("属性模板", eo.AtlName, this.Actor, Request);
                Response.Redirect("List.aspx?cid=" + this.hid_CategoryKey.Value + "&t=" + AtlKey);
            } else {//更新----------------------------------
                TProductAtlEO eo = biz.Load<TProductAtlEO>(AtlKey);
                eo.AtlDesc = this.txt_AltDesc.Text;
                eo.AtlName = this.txt_AtlName.Text;
                eo.CategoryKey = this.hid_CategoryKey.Value;
                eo.DefaultValue = this.txt_DefaultVaue.Text;
                eo.FieldType = this.ddl_FieldType.SelectedValue;
                eo.IsQueriable = Convert.ToInt32(this.ral_Queriable.SelectedValue);
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                biz.Update<TProductAtlEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("属性模板", eo.AtlName, this.Actor, Request);
                Response.Redirect("List.aspx?cid=" + this.hid_CategoryKey.Value + "&t=" + AtlKey);
            }
        }
    }
}
